package com.wyx.springbootconfig;

import com.wyx.utils.CommonResult;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName MyErrorController
 * @Description TODO
 * @Author 王玉星
 * @Date 2021/8/8 23:42
 * @Version 1.0
 */

@RestController
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public CommonResult handleError(HttpServletRequest request){
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if(statusCode == 401){
            return CommonResult.perNoAll();
        }else if(statusCode == 404){
            return CommonResult.isNull();
        }else if(statusCode == 403){
            return CommonResult.ErrorMethod();
        }else{
            return CommonResult.MyMessage("出现了未知异常，请联系管理员");
        }

    }
    @Override
    public String getErrorPath() {
        return "/error";
    }
}
